package com.cms.pms.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.cms.pms.PMSContext;
import com.cms.pms.Transaction;

public class TransactionImpl implements Transaction {
	
	private String signature;
	private int status = Transaction.SUCCESS;
	
	private long startTime;
	private long endTime;
	private long duration;
	
	
	private Thread thread;
	
	private Exception exception;
	
	private String tid;
	
	private Map<String, String> tagsMap ;
	
	
	public TransactionImpl(String signature)
	{
		this.signature = signature;
		this.startTime = System.currentTimeMillis();
		this.thread = Thread.currentThread();
	}
	
	@Override
	public void setStatus(int s) {
		this.status = s;
	}
	

	@Override
	public void complete() {
		this.endTime = System.currentTimeMillis();
		this.duration = this.endTime - this.startTime;
		PMSContext.getStore().put(this);
	}

	@Override
	public String signature() {
		return this.signature;
	}

	@Override
	public int getStatus() {
		return this.status;
	}

	@Override
	public long getStartTime() {
		return this.startTime;
	}

	@Override
	public long getEndTime() {
		return this.endTime;
	}

	@Override
	public long getDuration() {
		return this.duration;
	}

	@Override
	public Thread getThread() {
		return this.thread;
	}

	@Override
	public Exception getException() {
		return this.exception;
	}
	
	@Override
	public void setStatus(int s, Exception e) {
		this.status = s;
		this.exception = e;
	}

	@Override
	public void setTID(String tid) {
		this.tid = tid;
	}

	@Override
	public String getTID() {
		return this.tid;
	}

	@Override
	public Map<String, String> getTags() {
		return this.tagsMap;
	}

	@Override
	public void setTags(Map<String, String> tagsmap) {
		this.tagsMap = tagsmap;
	}

	@Override
	public void addTags(Map<String, String> tagsMap) {
		if(this.tagsMap == null)
			this.tagsMap = new  HashMap<String,String>();
		this.tagsMap.putAll(tagsMap);
	}
}
